﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyHospital.DAL;
namespace MyHospital.BLL
{
    public class BookService
    {
        //建立MyHospital.DAL数据访问层中的MyHospitalDataContext类实例db
        MyHospitalDataContext db = new MyHospitalDataContext();

        public List<MyHospital.DAL.Doctor> doctorShow(int departId)
        {
            var results = (from r in db.Doctor
                           where r.departId == departId
                           select r).ToList();

            return results;
        }

        public List<MyHospital.DAL.Doctor> doctorShow(string doctorName, string doctorJob)
        {
            var results = (from r in db.Doctor
                           where r.doctorName == doctorName && r.job == doctorJob
                           select r).ToList();
            return results;
        }

        /// <summary>
        /// 根据医生id得到医生的时间段信息
        /// </summary>
        /// <param name="doctorId"></param>
        /// <returns></returns>
        public List<MyHospital.DAL.TimePart> doctorTimeShow(int doctorId, DateTime date)
        {
            var results = (from d in db.Doctor
                           join t in db.TimePart on d.doctorId equals t.doctorId
                           where d.doctorId == doctorId && t.date == date
                           select t).ToList();
            return results;
        }

        /* public int test(String name)
         {
             var results = (from r in db.Doctor
                            where r.doctorName == name
                            select r).FirstOrDefault().doctorId;
             return results;
         }*/

        /// <summary>
        /// 模糊查找疾病名称
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<MyHospital.DAL.Disease> findDis(String name)
        {
            var results = (from r in db.Disease
                           where System.Data.Linq.SqlClient.SqlMethods.Like(r.disName, "%" + name + "%")
                           select r).ToList();
            return results;
        }
        public List<MyHospital.DAL.Disease> showDis()
        {
            var results = (from r in db.Disease
                           select r).ToList();
            return results;
        }
        /// <summary>
        /// 根据疾病的名称得到相应科室的医生信息
        /// </summary>
        /// <param name="disName"></param>
        /// <returns></returns>
        public List<MyHospital.DAL.Doctor> doctorShow(String disName)
        {
            var departId = (from r in db.Disease
                            where r.disName == disName
                            select r).FirstOrDefault().departId;
            var results = (from d in db.Doctor
                           where d.departId == departId
                           select d).ToList();
            return results;
        }
        /// <summary>
        /// 根据医生id得到医生信息
        /// </summary>
        /// <param name="doctorId"></param>
        /// <returns></returns>
        public List<MyHospital.DAL.Doctor> doctorShowOfId(int doctorId)
        {
            var results = (from r in db.Doctor
                           where r.doctorId == doctorId
                           select r).ToList();
            return results;
        }
        public List<MyHospital.DAL.Doctor> doctorShowOfdepName(String departName)
        {
            var results = (from d in db.Department
                           join r in db.Doctor on d.departId equals r.departId
                           where d.departName == departName
                           select r).ToList();
            return results;
        }

        /// <summary>
        /// 模糊查找科室名称
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public List<MyHospital.DAL.Department> findDepat(String name)
        {
            var results = (from r in db.Department
                           where System.Data.Linq.SqlClient.SqlMethods.Like(r.departName, "%" + name + "%")
                           select r).ToList();
            return results;
        }
        /// <summary>
        /// 显示科室的全部信息
        /// </summary>
        /// <returns></returns>
        public List<MyHospital.DAL.Department> showAllDepart()
        {
            var results = (from r in db.Department
                           select r).ToList();
            return results;
        }
        
        /// <summary>
        /// 将预约相应的信息插入表中
        /// </summary>
        /// <param name="patId"></param>
        /// <param name="doctorId"></param>
        /// <param name="time"></param>
        /// <param name="date"></param>
        public void insertBookInfor(int patId, int doctorId, String time, String date)
        {
            var departId = (from r in db.Doctor
                            where r.doctorId == doctorId
                            select r).FirstOrDefault().departId;
            BookingInfor bookingInfor = new BookingInfor();
            bookingInfor.patId = patId;
            bookingInfor.doctorId = doctorId;
            bookingInfor.departId = departId;
            bookingInfor.time = time;
            bookingInfor.date= date;
            db.BookingInfor.InsertOnSubmit(bookingInfor);
            db.SubmitChanges();
        }

        public bool bookUpdate(int doctorId, String time)
        {
            var timer = (from r in db.TimePart
                         where r.doctorId == doctorId && r.time == time
                         select r).FirstOrDefault().number;
            if(timer == 0)
            {
                return false;
            } else
            {
                var results = from r in db.TimePart
                              where r.doctorId == doctorId && r.time == time
                              select r;
                foreach (MyHospital.DAL.TimePart r in results)
                {
                    r.number = (timer - 1);
                }
                db.SubmitChanges();
                return true;
            }
        }

        public List<BookingInfor> bookInforOfPatId(int patId, String date)
        {
            var results = (from r in db.BookingInfor
                           where r.patId == patId && r.date == date
                           select r).ToList();
            return results;
        }
        /*  public List<BookInformation> bookInforOfPatId(int patId)
          {
              var results = (from r in db.BookingInfor
                             join d in db.Doctor on r.doctorId equals d.doctorId
                             join p in db.Department on r.departId equals p.departId
                             select new BookInformation(d.doctorName,
                                 p.departName,
                                 r.time,
                                 r.date)).ToList();
              return results;
          }*/
        /// <summary>
        /// 根据医生姓名得到医生id
        /// </summary>
        /// <param name="doctorName"></param>
        /// <returns></returns>
        public int findDoctorIdOfName(String doctorName)
        {
            var results = (from r in db.Doctor
                           where r.doctorName == doctorName
                           select r).FirstOrDefault().doctorId;
            return results;
        }
        /// <summary>
        /// 取消预约后，将该医生的该时间段的人数加一
        /// </summary>
        /// <param name="doctorId"></param>
        /// <param name="time"></param>
        /// <param name="date"></param>
        public void bookCancelAddNumber(int doctorId, String time, DateTime date)
        {
            var results = from r in db.TimePart
                          where r.doctorId == doctorId && r.time == time && r.date == date
                          select r;
            if (results != null)
            {
                foreach (TimePart t in results)
                {
                    t.number = t.number + 1;
                }
                db.SubmitChanges();
            }
        }
    }
}
